package org.jeecg.modules.iot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.iot.model.SocketPort;
import org.jeecg.modules.iot.mqtt.server.service.processor.socketOperate.ChargePileProcessor;
import org.jeecg.modules.iot.service.ISocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/iot/socket")
@Api(tags = "插座服务接口")
public class SocketController {

    @Autowired
    private ISocketService socketService;


    @Autowired
    private ChargePileProcessor chargePileProcessor;



    @ApiOperation("添加插座")
    @PostMapping("/add")
    public int add(@RequestBody SocketPort socket) {
        return socketService.add(socket);
    }

    @ApiOperation("打开插座端口-用户")
    @PostMapping("/openPort/{deviceCode}/{port}/{time}/{power}/{autoOpen}/{openId}")
    public Result openPort(@PathVariable String deviceCode, @PathVariable int port, @PathVariable int time,
                           @PathVariable int power, @PathVariable int autoOpen, @PathVariable String openId) {
        return chargePileProcessor.openPort(deviceCode, port, time, power, autoOpen, openId);
    }


    @ApiOperation("关闭插座端口-用户")
    @PostMapping("/closePort")
    public Integer closePort(@RequestParam("deviceCode") String deviceCode,
                             @RequestParam("port") int port) {
        return chargePileProcessor.closePort(deviceCode, port);
    }
    @ApiOperation("更新插座")
    @PutMapping("/update")
    public int update(@RequestBody SocketPort socket) {
        return socketService.update(socket);
    }

    @ApiOperation("删除插座")
    @DeleteMapping("/delete")
    public int delete(@RequestParam("portId") Integer portId,
                      @RequestParam("deviceCode") Long deviceCode) {
        return socketService.delete(portId, deviceCode);
    }

    @ApiOperation("根据设备编码查询插座")
    @GetMapping("/queryByDeviceCode")
    public SocketPort queryByDeviceCode(@RequestParam("portId") Integer portId,
                                        @RequestParam("deviceCode") String deviceCode) {
        return socketService.queryByDeviceCode(portId, deviceCode);
    }
}